package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.inventory.entity.Lot;
import com.mxpio.erp.common.inventory.service.LotPropService;
import com.mxpio.erp.common.inventory.service.LotService;
import com.mxpio.erp.common.inventory.service.ProdSerialNumberService;
import com.mxpio.erp.common.vo.InvFlowSummaryVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "LotController", description = "批次接口")
@RestController
@RequestMapping("/erp/inventory/lot/")
public class LotController {
	
	@Autowired
	private LotService lotService;
	
	@Autowired
	private LotPropService lotPropService;
	
	@Autowired
	private ProdSerialNumberService prodSerialNumberService;

	@GetMapping("list")
	@Operation(summary = "批次列表", description = "获取批次列表", method = "GET")
	public Result<List<Lot>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<Lot> lots = lotService.list(Lot.class, criteria);
		return Result.OK(lots);
	}
	
	@GetMapping("page")
	@Operation(summary = "批次列表", description = "获取批次列表", method = "GET")
	public Result<Page<Lot>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Lot> page = lotService.listPage(Lot.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{id}")
	@Operation(summary = "根据id获取批次", description = "根据id获取批次", method = "GET")
	public Result<Lot> getById(@PathVariable(name = "id", required = true) String id) {
		Lot lot = lotService.getById(Lot.class, id);
		return Result.OK(lot);
	}
	
	@GetMapping("executed/{bizNo}/{lineNo}/{accessType}")
    @Operation(summary = "获取已执行批次", description = "获取已执行批次", method = "GET")
	public Result<Collection<InvFlowSummaryVo>> getLot(
			@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,
			@Parameter(description = "行号") @PathVariable(name = "lineNo", required = true) String lineNo,
			@Parameter(description = "出库类型") @PathVariable(name = "accessType", required = true) String accessType,
			@Parameter(description = "仓库编号") @RequestParam(name = "whCode", required = false) String whCode) {
		Collection<InvFlowSummaryVo> flows = lotService.getExecutedInvFlows(bizNo, lineNo, accessType, whCode);
		return Result.OK(flows);
	}
	
}
